﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace Red.Scripts.Defines.Log
{
    public delegate void LogHandler( string text );

    public abstract class Emitter
    {
        #region Protected Methods

        protected void WriteLog( string text )
        {
            Debug.Print( text );
            Log?.Invoke( text );
        }

        #endregion Protected Methods

        #region Events

        public event LogHandler Log;

        #endregion Events
    }

    public abstract class Propagator
    {
        #region Private Data

        private ICollection<Emitter> _emitters = new List<Emitter>();

        #endregion Private Data

        #region Public Methods

        public void Add( Emitter emitter )
        {
            _emitters.Add( emitter );
        }

        #endregion Public Methods

        #region Events

        public event LogHandler Log
        {
            add
            {
                foreach ( Emitter emitter in _emitters )
                    emitter.Log += value;
            }
            remove
            {
                foreach ( Emitter emitter in _emitters )
                    emitter.Log -= value;
            }
        }

        #endregion Events
    }
}
